package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.Util.HttpUtil;
import com.example.Util.Md5Util;
import com.example.Util.Tools;
import com.example.pojo.accountProperties;
import com.example.service.MerchProviderQueryService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Component
public class MerchProviderQueryServiceImpl implements MerchProviderQueryService {

    @Autowired
    private accountProperties AccountProperties;

    @Autowired
    private com.example.pojo.UrlMethodProperties UrlMethodProperties;

    @Override
    public String MerchProviderQuery(JSONObject reqJson) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        JsonNode jsonNode = objectMapper.readTree(reqJson.toString());


        /**
         *  公共请求参数
         */

        //具体业务接口名称
        String methed = UrlMethodProperties.getMerchProviderQuery();
        //服务商商户号
        String isv_mch_uid = AccountProperties.getIsv_mch_uid();
        //服务商的应用id
        String isv_app_id = AccountProperties.getIsv_app_id();
        //密钥md5
        String key = AccountProperties.getKey();
        //版本号,默认1.0
        String version = "1.0";
        //编码格式默认为UTF-8
        String charset = "UTF-8";
        //	商户生成签名字符串所使用的签名算法类型,目前固定MD5
        String sign_type = "MD5";
        //发送请求的时间 yyyyMMddHHmmss
        String timestamp = Tools.GetTimeStamp14Time();

        //组装签名串得出签名结果
        Map<String, String> map = new HashMap<>();
        map.put("method", methed);
        map.put("version", version);
        map.put("isv_mch_uid", isv_mch_uid);
        map.put("isv_app_id", isv_app_id);
        map.put("charset", charset);
        map.put("sign_type", sign_type);
        map.put("timestamp", timestamp);
        map.put("biz_content", String.valueOf(jsonNode));

        String a = Tools.mapDataSortAToZ(map);
        String sign = Md5Util.getMD5(a + "&key=" + key);


        log.info("签名串：--------------------------------->>>{}", a + "&key=" + key);

        //组装最终请求参数

        Map<String, String> reqMap = new HashMap<>();
        reqMap.put("method", methed);
        reqMap.put("version", version);
        reqMap.put("isv_mch_uid", isv_mch_uid);
        reqMap.put("isv_app_id", isv_app_id);
        reqMap.put("charset", charset);
        reqMap.put("sign_type", sign_type);
        reqMap.put("timestamp", timestamp);
        reqMap.put("biz_content", String.valueOf(jsonNode));
        reqMap.put("sign", sign);

        log.info("请求参数：--------------------------------->>>{}", JSON.toJSONString(reqMap));

        //请求地址
        String url = UrlMethodProperties.getMerchProviderQuery();

        String result = HttpUtil.sendPostJson(url, reqMap);

        log.info("返回结果：--------------------------------->>>{}", result);



        return result;
    }


}
